package cn.lyq.card.base

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import cn.lyq.card.R
import cn.lyq.card.utils.LoadingDialog
import cn.lyq.card.utils.QMUIStatusBarHelper
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.BusUtils
import java.lang.Class as Class1


abstract class BaseActivity : AppCompatActivity() {

    private val loadingDialog by lazy {
        LoadingDialog.Builder(this).setCancelOutside(false).setCancelable(true).create()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        openImmersedStateBar(isImmersedStateBarNeeded())

        BusUtils.register(this)

        setContentView(getLayoutId())

        initVM()
        initData()
        initView()
        initListener()
        initObserver()
    }

    override fun onStart() {
        super.onStart()
        if (isImmersedStateBarNeeded()) {
            QMUIStatusBarHelper.getStatusbarHeight(this)
                .takeIf { it > 0 }?.let {
                    findViewById<View>(R.id.vTop)?.run {
                        layoutParams.height = it
                        layoutParams = layoutParams
                    }
                }
        }
    }

    override fun onResume() {
        super.onResume()
        setBarColor(isLightBarColor())
    }

    open fun isImmersedStateBarNeeded() = true

    open fun isLightBarColor() = false

    fun setBarColor(white: Boolean = false) {
        if (white) {
            QMUIStatusBarHelper.setStatusBarDarkMode(this)
        } else {
            QMUIStatusBarHelper.setStatusBarLightMode(this)
        }
    }

    fun startActivity(clazz: Class1<*>) {
        startActivity(Intent(this, clazz))
    }

    fun setMyTitle(title: String) {
        findViewById<TextView>(R.id.tvTitle)?.text = title
    }

    fun setMyTitle(title: Int) {
        findViewById<TextView>(R.id.tvTitle)?.setText(title)
    }

    open fun openImmersedStateBar(open: Boolean = false) {
        if (open) {
            QMUIStatusBarHelper.translucent(window)
        }
    }

    abstract fun getLayoutId(): Int

    open fun initVM() {}

    open fun initData() {}

    open fun initView() {
        val imgBck = findViewById<ImageView>(R.id.imgBack)
        imgBck?.setClick {
            onBackPressed()
        }
    }

    open fun initListener() {}

    open fun initObserver() {}

    fun <T : ViewModel> getVM(
        vm: Class1<T>,
        factory: ViewModelProvider.Factory = ViewModelProvider.NewInstanceFactory()
    ) = ViewModelProvider(this, factory).get(vm)

    override fun onDestroy() {
        super.onDestroy()
        BusUtils.unregister(this)
    }

    fun showLoading() {
        loadingDialog.show()
    }

    fun dismissLoading() {
        if (loadingDialog.isShowing) {
            loadingDialog.dismiss()
        }
    }
}